﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Av.Utils;

namespace FileSyncer
{
    public class FileWatcher
    {

        protected FileSystemWatcher m_watcher = new FileSystemWatcher();

        protected List<string> m_created = new List<string>();
        protected List<string> m_changed = new List<string>();
        protected List<string> m_deleted = new List<string>();

        /// <summary>
        /// Gets list of changed files
        /// </summary>
        public List<string> Changed { get { return m_changed; } }

        public void Start(string dir)
        {
            Log4cs.Log("File watcher is watching {0}...", dir);

            m_watcher.IncludeSubdirectories = true;
            m_watcher.Path = dir;
            m_watcher.EnableRaisingEvents = true;

            m_watcher.Changed += new FileSystemEventHandler(OnFileSystemChanged);
            m_watcher.Created += new FileSystemEventHandler(OnFileSystemCreated);
            m_watcher.Deleted += new FileSystemEventHandler(OnFileSystemDeleted);
        }

        void OnFileSystemDeleted(object sender, FileSystemEventArgs e)
        {
            Log4cs.Log("File {0} was deleted...", e.FullPath);
            if( !m_deleted.Contains(e.FullPath) )
            {
                m_deleted.Add(e.FullPath);
            }
        }

        void OnFileSystemCreated(object sender, FileSystemEventArgs e)
        {
            Log4cs.Log("File {0} was created...", e.FullPath);
            if(!m_created.Contains(e.FullPath))
            {
                m_created.Add(e.FullPath);
            }
        }

        void OnFileSystemChanged(object sender, FileSystemEventArgs e)
        {
            Log4cs.Log("File {0} was changed...", e.FullPath);
            if(!m_changed.Contains(e.FullPath))
            {
                m_changed.Add(e.FullPath);
            }
        }

    }
}
